package com.itdianzi.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.controller.util.FileUploadUtil;
import com.itdianzi.domain.cargo.*;
import com.itdianzi.service.cargo.ContractService;
import com.itdianzi.service.cargo.ExtCproductService;
import com.itdianzi.service.cargo.FactoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ContractService contractService;
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(String contractId , String contractProductId, @RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size) {
    //根据货物ID查询
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, page, size);

        request.setAttribute("page", pageInfo);


        //查询附件的生产厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        //存入货物ID
        request.setAttribute("contractId",contractId);
        //存入合同ID
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }

    @Autowired
    private FileUploadUtil fileUploadUtil;
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct, String contractId, MultipartFile productPhoto) throws Exception {

        if (StringUtils.isBlank(extCproduct.getId())) {
        /*    if (productPhoto != null) { //进行文件上传  别人已经选了图片对象
                String imagePath = fileUploadUtil.upload(productPhoto);
                //添加成功后返回一个互联网图片的路径
                extCproduct.setProductImage(imagePath);
            }*/

            extCproductService.save(extCproduct);
        } else {

            extCproductService.update(extCproduct);

        }
        return  "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId ,String contractProductId) {

        //附件对象
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        //生产厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria = factoryExample.createCriteria();
        criteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);
        return "cargo/extc/extc-update";
    }

    @RequestMapping("/delete")
    public String delete(String id, String contractId, String contractProductId) {

        extCproductService.delete(id);

        return "redirect:/cargo/extCproduct/list.do?contractId=\"+contractId+\"&contractProductId="+contractProductId;
    }

}
